﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_user_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
        if (!IsPostBack)
        {
            if (null != Request.QueryString["id"])
            {
                CAdmin admin = new CAdmin();
                DataRow r = admin.ReadUser(int.Parse(Request.QueryString["id"].ToString()));
                this.tbName.Text = r["username"].ToString();
                this.tbPwd.Text = r["userpwd"].ToString();
                if (r["userrole"].ToString() == "0")
                {
                    this.rbtnAdmin.Checked = true;
                    this.rbtnCommon.Checked = false;
                }
                else
                {
                    this.rbtnAdmin.Checked = false;
                    this.rbtnCommon.Checked = true;
                }
            }
        }
    }

    void btnSubmit_Click(object sender, EventArgs e)
    {
        CAdmin admin = new CAdmin();
        if (null != Request.QueryString["id"])
        {            
            if (admin.ResetUserInfo(int.Parse(Request.QueryString["id"].ToString()),this.tbName.Text,this.tbPwd.Text,(this.rbtnAdmin.Checked ? "0" : "1")))
            {
                ScriptManager.RegisterClientScriptBlock(Page, upEdit.GetType(), "", "alert('操作完成！');", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, upEdit.GetType(), "", "alert('操作失败！');", true);
            }
        }
        else
        {
            if (admin.AddUser(this.tbName.Text,this.tbPwd.Text,(this.rbtnAdmin.Checked ? "0" : "1")))
            {
                ScriptManager.RegisterClientScriptBlock(Page, upEdit.GetType(), "", "alert('操作完成！');", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, upEdit.GetType(), "", "alert('操作失败！');", true);
            }
        }
    }
}